//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.standard.service.impl;

import com.ruoyi.live.app.service.CacheService;
import com.ruoyi.live.base.domain.LiveExamObjectiveScoreDetail;
import com.ruoyi.live.base.domain.LiveExamUser;
import com.ruoyi.live.base.domain.LiveScoreStandard;
import com.ruoyi.live.base.domain.LiveTaskUser;
import com.ruoyi.live.base.mapper.LiveExamObjectiveScoreDetailMapper;
import com.ruoyi.live.base.mapper.LiveProductSourceMapper;
import com.ruoyi.live.base.mapper.LiveTaskUserDetailMapper;
import com.ruoyi.live.standard.service.CountResult;
import com.ruoyi.live.standard.service.StandardService;
import com.ruoyi.live.tool.commons.IConvert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Service
public class ThreeStandardImpl implements StandardService {
    private static final Logger log = LoggerFactory.getLogger(ThreeStandardImpl.class);
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    LiveProductSourceMapper sourceDao;
    @Autowired
    LiveTaskUserDetailMapper detailDao;
    @Autowired
    CacheService cacheService;
    @Autowired
    LiveExamObjectiveScoreDetailMapper scoreDetailDao;

    public ThreeStandardImpl() {
    }

    public String getStandardCode() {
        return "SCORE_RULE_PRODUCT_THREE";
    }

    public CountResult doCountScore(LiveExamUser examUser, Long scoreId) {
        String sql = "select count(1) count, sum(answer) answer from live_product_source  where id in (select a.product_source_id from live_task_user_detail a where a.task_user_id = ? and a.detail_type = 1)";
        LiveScoreStandard standard = this.cacheService.getStandardByCode(this.getStandardCode());
        List<LiveTaskUser> tasks = examUser.getTaskUserList();
        Iterator var6 = tasks.iterator();

        while(var6.hasNext()) {
            LiveTaskUser task = (LiveTaskUser)var6.next();
            if (task.getTaskType() == 1) {
                LiveExamObjectiveScoreDetail scoreDetail = (new LiveExamObjectiveScoreDetail()).setTaskUserId(task.getId()).setScoreId(scoreId).setState(1).setProductId(task.getProductId());

                try {
                    if (standard == null) {
                        scoreDetail.setState(2).setErrDesc("获取评分标准失败,code为SCORE_RULE_PRODUCT_TWO");
                        this.scoreDetailDao.insert(scoreDetail);
                        break;
                    }

                    scoreDetail.setStandardId(standard.getId()).setStandardName(standard.getStandardName());
                    List<Map<String, Object>> res = this.jdbcTemplate.queryForList(sql, new Object[]{task.getId()});
                    if (res.size() > 0) {
                        Integer count = IConvert.object2int(((Map)res.get(0)).get("count"));
                        Integer answer = IConvert.object2int(((Map)res.get(0)).get("answer"));
                        if (count >= IConvert.object2int(this.cacheService.getParam("SYSTEM_PRODUCT_SOURCE_COUNT")) && answer == count) {
                            scoreDetail.setHasScore(1).setScore(standard.getStandardValue());
                        } else {
                            scoreDetail.setHasScore(2).setScore(new BigDecimal(0));
                        }
                    } else {
                        scoreDetail.setState(2).setErrDesc("计算得分出错，SCORE_RULE_PRODUCT_THREE");
                    }

                    this.scoreDetailDao.insert(scoreDetail);
                } catch (Exception var12) {
                    var12.printStackTrace();
                    log.error("LiveExamUser的id为{}计算得分出错，SCORE_RULE_PRODUCT_THREE", examUser.getId(), var12);
                    scoreDetail.setState(2).setErrDesc("计算得分出错，code为SCORE_RULE_PRODUCT_THREE");
                    this.scoreDetailDao.insert(scoreDetail);
                }
            }
        }

        return new CountResult(0.0D);
    }
}
